package com.mtsmda.jersey.javapoint;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(value = "/palindrom")
public class Palindrom {

	private boolean isPalindrom = false;

	public StringBuilder findPalindrom(String palindrom) {
		StringBuilder stringBuilder = null;
		if (palindrom != null && !palindrom.isEmpty()) {
			stringBuilder = new StringBuilder(palindrom);
			if (palindrom.equals(stringBuilder.reverse().toString())) {
				this.isPalindrom = true;
			} else {
				this.isPalindrom = false;
			}

		}
		stringBuilder.append(" - ").append(
				new StringBuffer(palindrom).reverse());
		return stringBuilder;
	}

	@GET
	@Produces(value = { MediaType.TEXT_PLAIN })
	@Path("")
	public String getPalindromPlainText(String palindrom) {
		StringBuilder findPalindrom = findPalindrom(palindrom);
		return findPalindrom.toString();
	}

	@GET
	@Produces(value = { MediaType.TEXT_XML })
	public String getPalindromXml(String palindrom) {
		StringBuilder builder = new StringBuilder(
				"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		builder.append("\n").append("<document>").append("<isPalindrom>")
				.append(this.isPalindrom).append("</isPalindrom>")
				.append("<text>").append(findPalindrom(palindrom))
				.append("</text>").append("</document>");
		return builder.toString();
	}

	@GET
	@Produces(value = { MediaType.TEXT_HTML })
	public String getPalindromHtml(String palindrom) {
		StringBuilder builder = new StringBuilder("<html><head><title>");
		builder.append("text html rest ws").append("</title></head><body>");
		builder.append("<h1>Is palindrom - ").append(this.isPalindrom)
				.append("</h1>");
		builder.append("<p>").append(findPalindrom(palindrom)).append("</p>");
		builder.append("</body></html>");
		return builder.toString();
	}

}